package br.com.adimoveis.util.ui;

import java.awt.Color;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.swing.JCheckBox;
import javax.swing.JPanel;

import org.jdesktop.swingx.VerticalLayout;


public class CheckList extends JPanel{
	
	private static final long serialVersionUID = 1L;

	private Map<JCheckBox, Object> mapa;
	
	public CheckList(){
		super(new VerticalLayout());
		setBackground(Color.WHITE);
		mapa = new HashMap<JCheckBox, Object>();
		
		VerticalLayout layout = (VerticalLayout) getLayout();
		layout.setGap(1);
	}

	
	public CheckList(Collection<?> allElements, Collection<?> selectedElements){
		this();
		addElements(allElements,selectedElements);
	}
	
	public List<?> getSelectedElements(){
		List<Object> selectedItems = new ArrayList<Object>();
		for(JCheckBox item : mapa.keySet()){
			if(item.isSelected()){
				selectedItems.add(mapa.get(item));
			}
		}
		return selectedItems;
	}
	
	public void addElements(Collection<?> allElements, Collection<?> selectedElements){
		mapa.clear();
		removeAll();
		for(Object element : allElements){
			JCheckBox item = new JCheckBox(element.toString());
			item.setBackground(Color.WHITE);
			boolean isSelected = selectedElements.contains(element);
			
			item.setSelected(isSelected);
			mapa.put(item, element);
			add(item);
		}
		
		repaint();
		revalidate();
	}
	
	@Override
	public void setEnabled(boolean enabled) {
		super.setEnabled(enabled);
		for(JCheckBox cb : mapa.keySet()){
			cb.setEnabled(enabled);
		}
	}
	

}
